package com.huluobo.high3videoproject;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.huluobo.base.view.BaseActivity;
import com.huluobo.base.view.Event;
import com.huluobo.high3videoproject.fragment.LiveFragment;
import com.huluobo.high3videoproject.fragment.MineFragment;
import com.huluobo.high3videoproject.fragment.MainFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends BaseActivity implements BottomBarLayout.OnItemSelectedListener {
    //fragment集合
    private ArrayList<Fragment> fragments;
    private BottomBarLayout bottomBarLayout;
    //用于页面缓存 Integer代表下标,Fragment代表页面
    private final Map<Integer, Fragment> fragmentMap = new HashMap<>();

    @Override
    protected void handEvent(Event event) {
        super.handEvent(event);
        if (event.getType()==105){
            finish();
        }
    }

    protected void initData() {
        fragments = new ArrayList<>();
        fragments.add(new MainFragment());//主Fragment,用于展示视频列表页面和附近页面
        fragments.add(new LiveFragment());//用于展示直播页面
        fragments.add(new MineFragment());//用于展示我的页面

        bottomBarLayout.setCurrentItem(0);

        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA, RECORD_AUDIO,
                READ_PHONE_STATE,}, 101);
    }

    protected void initView() {
        bottomBarLayout = findViewById(R.id.main_bottom_layout);
        bottomBarLayout.setOnItemSelectedListener(this);
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_main;
    }

    //fragment上次展示的下标
    private int myPreviousPosition = -1;

    //Fragment 缓存策略,可以从map中读取之前的页面
    @Override
    public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
        //展示页面当前的下标
        //构建页面,查找map中有没有当前的页面
        //currentPosition 相当于map的键
        if (!fragmentMap.containsKey(currentPosition)) {
            //如果没有,在map中保存当前的fragment
            fragmentMap.put(currentPosition, fragments.get(currentPosition));
        }

        //判断当前的页面是否在map中添加过
        if (fragmentMap.get(currentPosition).isAdded()) {
            //map中添加过页面缓存,展示出来
            if (myPreviousPosition > -1) {
                //展示当前页面,隐藏上一个页面
                getSupportFragmentManager().beginTransaction()
                        .show(fragmentMap.get(currentPosition))//显示当前页面
                        .hide(fragmentMap.get(myPreviousPosition)).commit();//隐藏之前的页面
            }
        } else {
            //map中没有添加过页面
            if (myPreviousPosition > -1) {
                //证明之前的页面中有值,可以隐藏上一个页面
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.main_frameLayout, fragmentMap.get(currentPosition))
                        .hide(fragmentMap.get(myPreviousPosition)).commit();
            } else {
                //map中没有添加过页面,不要隐藏上一个页面,因为没有
                getSupportFragmentManager().beginTransaction().add(R.id.main_frameLayout,
                        fragmentMap.get(currentPosition)).commit();
            }
        }
        myPreviousPosition = currentPosition;

    }

}